﻿using ASmile.DBModel;
using ASmile.Extend;
using ASmile.ORM.Cores;

namespace ASmile.Com.Da
{
    public class WordCodeBLL : BLLCrudBase<Dict_WordCode>, IBLLCrud<Dict_WordCode>
    {
        //TODO: 如果不能删除，则需要重写，否则不用
        //public override ApiResult Del(Dict_WordCode model)
        //{
        //    var r = DB.Delete<Dict_WordCode>(s => s.WordName == wordName);
        //    if (r.RowCount == 1)
        //    {
        //        return RetOK("删除成功");
        //    }
        //    else
        //    {
        //        return RetErr("删除失败");
        //    }
        //}

        protected override void SetQuery(QueryCondition<Dict_WordCode> query, ArgPageData arg)
        {
            if (!arg.SearchVal.IsEmpty())
            {
                query.WhereAnd(s =>
                    s.WordName == arg.SearchVal ||
                    s.WordPY == arg.SearchVal ||
                    s.WordWB == arg.SearchVal
                    );
            }
        }

        protected override ApiResult<string> VerifyModel(Dict_WordCode model, bool isAdd)
        {
            if (model.WordName.IsEmpty())
            {
                return RetErr(nameof(model.WordName), "字不能为空");
            }
            if (model.WordName.Length != 1)
            {
                return RetErr(nameof(model.WordName), "长度必须为1");
            }
            if (isAdd && DB.QueryExist<Dict_WordCode>(s => s.WordName == model.WordName))
            {
                return RetErr(nameof(model.WordName), "该字已经存在");
            }
            if (!isAdd) model.ClearChangedState(Dict_WordCode.CNWordName);

            return RetOK("", "保存成功");
        }
    }
}